package com.un.ebs.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.un.ebs.core.util.SecurityUtils;
import com.un.ebs.core.util.WebResult;
import com.un.ebs.core.util.WebUtils;
import com.un.ebs.sys.domain.ConfigureGroup;
import com.un.ebs.sys.dto.ConfigureGroupAddDto;
import com.un.ebs.sys.dto.ConfigureGroupUpdateDto;
import com.un.ebs.sys.mapper.ConfigureGroupMapper;
import com.un.ebs.sys.service.ConfigureGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author liubo
 * @since 2019-11-03
 */
@RestController
@RequestMapping("/configureGroup")
@Api(value = "sys-configure-group", description = "配置分组")
public class ConfigureGroupController {

    @Autowired
    private ConfigureGroupService service;

    @Autowired
    private ConfigureGroupMapper configureGroupMapper;

    @PostMapping("add")
    @ApiOperation(value = "add", notes = "添加")
    public WebResult save(@RequestBody ConfigureGroupAddDto req) {
        service.add(req);
        return WebUtils.success();
    }

    @PostMapping("update")
    @ApiOperation(value = "update", notes = "修改")
    public WebResult update(@RequestBody ConfigureGroupUpdateDto req) {
        service.update(req);
        return WebUtils.success();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "getById", notes = "根据主键获取单个 ConfigureGroup")
    public WebResult getById(@PathVariable("id") String id) {
        return WebUtils.success(service.queryById(id));
    }

    @GetMapping("list")
    @ApiOperation(value = "list", notes = "查询当前应用下的所有配置分组")
    public WebResult list() {
        LambdaQueryWrapper<ConfigureGroup> lq = Wrappers.lambdaQuery();
        lq.eq(ConfigureGroup::getAppId, SecurityUtils.getUserContent().getAppId());
        lq.eq(ConfigureGroup::getIsDelete, false);
        lq.orderByAsc(ConfigureGroup::getSort);
        return WebUtils.success(configureGroupMapper.selectList(lq));
    }

    @PostMapping("changeSort")
    @ApiOperation(value = "changeSort", notes = "修改 ConfigureGroup 的序号")
    public WebResult changeSort(@RequestParam String id, @RequestParam int index) {
        service.changeSort(id, index);
        return WebUtils.success();
    }

    @PostMapping("delete/{id}")
    @ApiOperation(value = "delete", notes = "根据主键删除单个 ConfigureGroup")
    public WebResult delete(@PathVariable("id") String id) {
        service.delete(id);
        return WebUtils.success();
    }

}